import process from "node:process";
import { fileURLToPath } from "node:url";

import Vue from "@vitejs/plugin-vue";
import VueJSX from "@vitejs/plugin-vue-jsx";
import UnoCSS from "unocss/vite";
import AutoImport from "unplugin-auto-import/vite";
import ElementPlus from "unplugin-element-plus/vite";
import IconsResolver from "unplugin-icons/resolver";
import UnpluginIcons from "unplugin-icons/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import AutoComponents from "unplugin-vue-components/vite";
import { VueRouterAutoImports } from "unplugin-vue-router";
import VueRouter from "unplugin-vue-router/vite";
import { defineConfig, loadEnv } from "vite";
// import VueDevTools from 'vite-plugin-vue-devtools';

import { generateIconCollection } from "./internals/svg-icon";
import { createProxy } from "./vite.proxy";

import { fileURLToPath, URL } from "node:url";

import cesium from "vite-plugin-cesium";

//自定义图标路径
const CUSTOM_ICON_PATH = fileURLToPath(new URL("./icons", import.meta.url));
const SRC_PATH = fileURLToPath(new URL("./src", import.meta.url));

// https://vitejs.dev/config/
export default defineConfig((config) => {
	const env = loadEnv(config.mode, process.cwd());

	const iconsResolver = IconsResolver({
		prefix: "i",
		customCollections: ["custom"],
	});

	return {
		base: env.VITE_PUBLIC_PATH, //系统部署路径
		resolve: {
			alias: {
				"@": SRC_PATH,
			},
		},
		server: {
			port: 8099,
			proxy: createProxy(config),
		},
		plugins: [
			VueRouter({
				routesFolder: ["src/pages"],
				exclude: ["./src/pages/**/components/*"],
				extensions: [".page.vue"],
			}),

			cesium(),

			// VueDevTools(),

			//   VueDevTools(),

			Vue(),
			VueJSX(),
			UnoCSS(),
			UnpluginIcons({
				autoInstall: true,
				customCollections: {
					//自定义图标
					custom: generateIconCollection(CUSTOM_ICON_PATH, {
						multiColor: true,
						varPrefix: "custom",
					}),
				},
			}),

			AutoImport({
				resolvers: [ElementPlusResolver(), iconsResolver],
				imports: [
					"vue",
					// 'vue-router',
					VueRouterAutoImports,
					"pinia",
					"@vueuse/core",
					{
						dayjs: [["default", "dayjs"]],
						axios: [["default", "axios"]],
						echarts: [["*", "echarts"]],
					},
				],
			}),
			AutoComponents({
				dirs: ["src/components"],
				resolvers: [
					ElementPlusResolver(),
					iconsResolver,

					(name) => {
						if (name === "VueEcharts") {
							return {
								name: "default",
								from: "vue-echarts",
							};
						}
					},
				],
			}),
			ElementPlus({}),
		],
	};
});
